﻿using Coldairarrow.Entity.Backstage.TheRules;
using Coldairarrow.Entity.Backstage.UserData;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Backstage.UserData
{
    public class BS_UserRebateBusiness : BaseBusiness<BS_UserRebate>, IBS_UserRebateBusiness, ITransientDependency
    {
        readonly Operator _operator;
        public BS_UserRebateBusiness(IDbAccessor db, Operator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<GetReabteDto>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = from a in GetIQueryable().Where(x=>x.SUserId.Equals(_operator.UserId))
                    select new GetReabteDto
                    { 
                        Id = a.Id,
                        Amount = a.ReturnMoney,
                        Purpose = a.Note,
                        Type = a.Type,
                        Time = a.CreateTime
                    };
            var where = LinqHelper.True<GetReabteDto>();

            return await q.Where(where).GetPageResultAsync(input);
        }

        public object GetSumData()
        {
            //总收益
            decimal sumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 0).Sum(x => x.ReturnMoney);
            //退回收益
            decimal ReturnData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 1).Sum(x => x.ReturnMoney);
            //已提现收益
            decimal ReflectData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 2).Sum(x => x.ReturnMoney);
            //获取体现规则列表
            int tDay = 30; //体现间隔天数
            decimal maxMoey = 15000; //每日最大提现额度

            var rMList = Db.GetIQueryable<BS_ReflectRule>().OrderByDescending(x => x.CreateTime).ToList();
            if (rMList.Count > 0)
            {
                tDay = rMList[0].ReflectDay;
                maxMoey = rMList[0].ReflectMoney;
            }

            //获取所有可提现额度
            var theTime = DateTime.Now;
            var sTime = DateTime.Now.AddDays(-1 * tDay);
            decimal kSumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 0 && x.CreateTime <= sTime).Sum(x => x.ReturnMoney);
            decimal stkSumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 1 && x.CreateTime <= sTime).Sum(x => x.ReturnMoney);

            //获取今天已提现额
            var strTime = DateTime.Parse(theTime.ToString("yyyy-MM-dd"));
            var endTime = strTime.AddDays(1);
            decimal jySumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 2 && x.CreateTime >= strTime && x.CreateTime < endTime).Sum(x => x.ReturnMoney);

            //获取今天可体现额度

            //可提现=总可提现-已提现-已退回
            decimal syktxData = kSumData - ReflectData - stkSumData;
            decimal jrktData = 0;
            if (syktxData > maxMoey)
            {
                jrktData = maxMoey;
            }
            else
            {
                jrktData = syktxData;
            }

            var data = new { 
                SumData = sumData, //总收益
                ReturnData, //已退回收益
                ReflectData, //已提现收益
                KTXData = syktxData, //总可提现收益
                DayMaxMoney = maxMoey, //每日体现总额度最大值
                JRTXData = jySumData, //今日已体现收益
                JRKTData = jrktData //今日可提现收益
            };

            return data;
        }

        /// <summary>
        /// 获取今日可提现金额
        /// </summary>
        /// <returns></returns>
        public decimal GetMyCanTransData()
        {
            //总收益
            decimal sumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 0).Sum(x => x.ReturnMoney);
            //退回收益
            decimal ReturnData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 1).Sum(x => x.ReturnMoney);
            //已提现收益
            decimal ReflectData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 2).Sum(x => x.ReturnMoney);
            //获取体现规则列表
            int tDay = 30; //体现间隔天数
            decimal maxMoey = 15000; //每日最大提现额度

            var rMList = Db.GetIQueryable<BS_ReflectRule>().OrderByDescending(x => x.CreateTime).ToList();
            if (rMList.Count > 0)
            {
                tDay = rMList[0].ReflectDay;
                maxMoey = rMList[0].ReflectMoney;
            }

            //获取所有可提现额度
            var theTime = DateTime.Now;
            var sTime = DateTime.Now.AddDays(-1 * tDay);
            decimal kSumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 0 && x.CreateTime <= sTime).Sum(x => x.ReturnMoney);
            decimal stkSumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 1 && x.CreateTime <= sTime).Sum(x => x.ReturnMoney);

            //获取今天已提现额
            var strTime = DateTime.Parse(theTime.ToString("yyyy-MM-dd"));
            var endTime = strTime.AddDays(1);
            decimal jySumData = GetIQueryable().Where(x => x.SUserId.Equals(_operator.UserId) && x.Type == 2 && x.CreateTime >= strTime && x.CreateTime < endTime).Sum(x => x.ReturnMoney);

            //获取今天可体现额度

            //可提现=总可提现-已提现-已退回
            decimal syktxData = kSumData - ReflectData - stkSumData;
            decimal jrktData = 0;
            if (syktxData > maxMoey)
            {
                jrktData = maxMoey;
            }
            else
            {
                jrktData = syktxData;
            }

            return jrktData;
        }

        public async Task<BS_UserRebate> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(BS_UserRebate data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(BS_UserRebate data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}